package knicker.api.servlet.action;

import java.io.IOException;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;

import knicker.api.servlet.auth.Authenticator;

import net.jeremybrooks.knicker.*;
import net.jeremybrooks.knicker.Knicker.ListType;
import net.jeremybrooks.knicker.dto.*;
import net.sf.json.JSONObject;

/**
 * Servlet implementation class NewWordListServlet
 */
public class AddWordListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Authenticator authenticator;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddWordListServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	try {
		System.out.println(Authenticator.getAuthToken());
			//Start 1. Get list of wordList.
			List<WordList> lists = AccountApi.wordLists(Authenticator.getAuthToken());
			TreeMap<String, String> mapLists = new TreeMap<String, String>();

			for(WordList ss : lists){
				mapLists.put(ss.getName(),ss.getPermalink()); System.out.println(ss.getName());}
			//End 1.
			
			//Start 2. Create new wordList.
			
			//Check list name for uniqueness
			if(mapLists.containsKey(request.getParameter("listName")))
				throw new KnickerException("not_unique");
			
			ListType type = ListType.PRIVATE;
			
			WordList newWordList = WordListApi.createList(Authenticator.getAuthToken(), request.getParameter("listName"), request.getParameter("desc"), type);
			
			
			HashMap<String, String> map = new HashMap<String, String>();
			
			map.put("createdAt", 		newWordList.getCreatedAt());
			map.put("description", 		newWordList.getDescription());
			map.put("id", 				newWordList.getId());
			map.put("name", 				newWordList.getName());
			map.put("permaLink", 		newWordList.getPermalink());
			map.put("updatedAt", 		newWordList.getUpdatedAt());
			map.put("userId", 			newWordList.getUserId());
			map.put("userName", 			newWordList.getUsername());
			map.put("type", 				newWordList.getType().toString());
			map.put("numberWordsInList", ((Integer)newWordList.getNumberWordsInList()).toString());
			map.put("status", "0");
			map.put("message", "success");
				// Convert to JSON string.
			JSONObject obj = new JSONObject();
			obj.putAll(map);
			  
			// Write JSON string.
			response.setContentType("application/json");
			response.setCharacterEncoding("UTF-8");
			response.getWriter().write(obj.toString());
		} catch (KnickerException e) {
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("status", "1");
			map.put("message", e.getMessage().toString());
			
			if(e.getMessage().toString().equals("Authentication token required.")){
				map.put("message", "token_required");
			}
			
			if(e.getMessage().toString().equals("List name required.")){
				map.put("message", "name_required");
			}
			if(e.getMessage().toString().equals("Description required.")){
				map.put("message", "desc_required");
			}
			
			JSONObject obj = new JSONObject();
			obj.putAll(map);
			  
			// Write JSON string.
			response.setContentType("application/json");
			response.setCharacterEncoding("UTF-8");
			response.getWriter().write(obj.toString());
		}
	}
}
